package com.hongyu.smart.village.service;


import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

import com.hongyu.smart.village.util.bluetooth.PrintUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

public class PrintService {
    private static final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");


    BluetoothSocket mmSocket;
    BluetoothDevice mmDevice;
    private  InputStream mmInStream;
    private  OutputStream mmOutStream;

    public PrintService(){




    }

    public void printTicket(){
        InputStream tmpIn = null;
        OutputStream tmpOut = null;
        //通过 socket 得到 InputStream 和 OutputStream
        try {
            tmpIn = mmSocket.getInputStream();
            tmpOut = mmSocket.getOutputStream();
        } catch (IOException e) {

        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
        PrintUtil.setOutputStream(mmOutStream);
        write();
        try {
            mmSocket.close();
        } catch (IOException e) {

        }


    }

    @SuppressLint("MissingPermission")
    public void
    connectPrinter(BluetoothDevice device){
        mmDevice=device;
        try {
            mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            // 耗时操作，所以必须在主线程之外进行
            mmSocket.connect();
            Log.i("AAAAAAAAAAAA",mmDevice.getName()+"连接成功");
        } catch (IOException connectException) {
            //处理连接建立失败的异常
            try {
                mmSocket.close();
            } catch (IOException closeException) { }
            return;
        }
    }
    public void write() {
        PrintUtil.selectCommand(PrintUtil.RESET);
        PrintUtil.selectCommand(PrintUtil.LINE_SPACING_DEFAULT);
        PrintUtil.selectCommand(PrintUtil.ALIGN_CENTER);
        PrintUtil.printText("美食餐厅\n\n");
        PrintUtil.selectCommand(PrintUtil.DOUBLE_HEIGHT_WIDTH);
        PrintUtil.printText("桌号：1号桌\n\n");
        PrintUtil.selectCommand(PrintUtil.NORMAL);
        PrintUtil.selectCommand(PrintUtil.ALIGN_LEFT);
        PrintUtil.printText(PrintUtil.printTwoData("订单编号", "201507161515\n"));
        PrintUtil.printText(PrintUtil.printTwoData("点菜时间", "2016-02-16 10:46\n"));
        PrintUtil.printText(PrintUtil.printTwoData("上菜时间", "2016-02-16 11:46\n"));
        PrintUtil.printText(PrintUtil.printTwoData("人数：2人", "收银员：张三\n"));

        PrintUtil.printText("--------------------------------\n");
        PrintUtil.selectCommand(PrintUtil.BOLD);
        PrintUtil.printText(PrintUtil.printThreeData("项目", "数量", "金额\n"));
        PrintUtil.printText("--------------------------------\n");
        PrintUtil.selectCommand(PrintUtil.BOLD_CANCEL);
        PrintUtil.printText(PrintUtil.printThreeData("面", "1", "0.00\n"));
        PrintUtil.printText(PrintUtil.printThreeData("米饭", "1", "6.00\n"));
        PrintUtil.printText(PrintUtil.printThreeData("铁板烧", "1", "26.00\n"));
        PrintUtil.printText(PrintUtil.printThreeData("一个测试", "1", "226.00\n"));
        PrintUtil.printText(PrintUtil.printThreeData("牛肉面啊啊", "1", "2226.00\n"));
        PrintUtil.printText(PrintUtil.printThreeData("牛肉面啊啊啊牛肉面啊啊啊", "888", "98886.00\n"));

        PrintUtil.printText("--------------------------------\n");
        PrintUtil.printText(PrintUtil.printTwoData("合计", "53.50\n"));
        PrintUtil.printText(PrintUtil.printTwoData("抹零", "3.50\n"));
        PrintUtil.printText("--------------------------------\n");
        PrintUtil.printText(PrintUtil.printTwoData("应收", "50.00\n"));
        PrintUtil.printText("--------------------------------\n");

        PrintUtil.selectCommand(PrintUtil.ALIGN_LEFT);
        PrintUtil.printText("备注：不要辣、不要香菜");
        PrintUtil.printText("\n\n\n\n\n");

    }


    


}
